package com.jiangxjsy.crm.service.impl;

import com.jiangxjsy.core.enums.FieldEnum;
import com.jiangxjsy.crm.mapper.CrmFieldExtendMapper;
import com.jiangxjsy.crm.service.CrmFieldExtendService;
import com.jiangxjsy.crm.support.CrmFieldExtendSupport;
import com.jiangxjsy.model.crm.bo.CrmFieldExtendBO;
import com.jiangxjsy.model.crm.entity.CrmFieldExtend;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * wukong-crm-master
 * 蒋胜宇
 * 2025/6/19 15:56
 */

@Service
public class CrmFieldExtendServiceImpl implements CrmFieldExtendService {
    @Resource
    private CrmFieldExtendMapper crmFieldExtendMapper;

    @Resource
    private CrmFieldExtendSupport crmFieldExtendSupport;

    /**
     * 查询字段扩展
     *
     * @param parentFieldId 父字段ID
     * @return 字段扩展
     */
    @Override
    public List<CrmFieldExtendBO> queryCrmFieldExtend(Integer parentFieldId) {
        List<CrmFieldExtend> fieldExtendList = crmFieldExtendMapper.selectListByParentFieldId(parentFieldId);
        List<CrmFieldExtendBO> fieldExtends = new ArrayList<>();
        for (CrmFieldExtend crmFieldExtend : fieldExtendList) {
            CrmFieldExtendBO crmFieldExtendBO = new CrmFieldExtendBO();
            BeanUtils.copyProperties(crmFieldExtend, crmFieldExtendBO);
            fieldExtends.add(crmFieldExtendBO);
        }

        fieldExtends.forEach(fieldExtend -> crmFieldExtendSupport.recordToFormType(
                fieldExtend, FieldEnum.parse(Math.toIntExact(fieldExtend.getType())))
        );
        return fieldExtends;
    }
}
